<?php

namespace app\admin\model;

use think\Model;


class Campaign extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'campaign';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'politic_text',
        'avatar_text'
    ];
    

    
    public function getPoliticList()
    {
        return ['1' => __('Politic 1'), '2' => __('Politic 2'), '3' => __('Politic 3')];
    }


    public function getPoliticTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['politic']) ? $data['politic'] : '');
        $list = $this->getPoliticList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAvatarTextAttr($value,$data){
        if(!empty($data['avatar']))
        $value = $value ? $value : (isset($data['avatar']) ? $data['avatar'] : '');
        $site = \think\Config::get("site");
        $preg = "/^http(s)?:\\/\\/.+/";
        if(!preg_match($preg,$value))
        {
            $value = $site['file_url'].$value;
        }
        return $value;
    }




    public function vote()
    {
        return $this->belongsTo('Vote', 'vote_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function site()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
